﻿local L = LibStub( "AceLocale-3.0" ):GetLocale( "SimpleActionSets" )

function SimpleActionSets:Actions_OnShow()
	self:Actions_Load( self:GetCurrentSet() );
end

function SimpleActionSets:Actions_OnHide()
	self:SetTemp(nil, nil, nil);
end

function SimpleActionSets:Actions_Load( set, plr )
	local SAS_Temp = {};
	local PlrName = self:GetPlrName();

	if ( not plr ) then
		plr = PlrName;
	end

	if ( not set or set == L["SAS_TEXT_CURRENT"] ) then
		set = nil;
		SAS_Temp = self:IterateActions();
		self:SetTemp(nil, nil, SAS_Temp);
		self:Debug( "Loading current actions" );
		UIDropDownMenu_Initialize(SASActionSetsDropDown, SimpleActionSets.Actions_DropDown_Initialize);
		UIDropDownMenu_SetSelectedID( SASActionSetsDropDown, 0 );
		SASActionSetsDropDownButton:Disable();
		SASActionSetsDropDownText:SetText( "|c00999999"..L["SAS_TEXT_CURRENT"] );
		SASActionsDelete:Disable();
	elseif ( plr ~= PlrName ) then
		SAS_Temp = self:CopyTable(self.db.profiles[plr].sets[set]);
		self:SetTemp(nil, nil, SAS_Temp);
		self:Debug( "Loading set "..set.." from "..plr );
		UIDropDownMenu_Initialize(SASActionSetsDropDown, SimpleActionSets.Actions_DropDown_Initialize);
		UIDropDownMenu_SetSelectedID( SASActionSetsDropDown, 0 );
		SASActionSetsDropDownText:SetText( "|c00999999"..L["SAS_TEXT_CURRENT"] );
		SASActionsDelete:Disable();
	else
		SAS_Temp = self:CopyTable(self.db.profile.sets[set]);
		self:SetTemp(nil, nil, SAS_Temp);
		self:Debug( "Loading set "..set );
		UIDropDownMenu_Initialize(SASActionSetsDropDown, SimpleActionSets.Actions_DropDown_Initialize);
		UIDropDownMenu_SetSelectedName( SASActionSetsDropDown, set );
		SASActionsDelete:Enable();
	end

	for k, v in pairs(self.db.profile.sets) do
		SASActionSetsDropDownButton:Enable();
		break;
	end

	self:Actions_Display();
	SASActionsSave:Disable();
end

function SimpleActionSets:Actions_Bar_CheckAll( self )
	-- Check all bars enabled or disabled
	local check = self:GetChecked();

	for i = 0, 9 do
		_G["SASActionBar"..i.."Enable"]:SetChecked(check);
		--SASActions_Bar_Toggle( i, check );
		if ( SimpleActionSets:GetTemp(i) ) then
			if ( check ) then
				SimpleActionSets:SetTemp(i , 0, nil);
			else
				SimpleActionSets:SetTemp(i, 0, 1);
			end
		end
	end
	SimpleActionSets:Actions_Display();
end

function SimpleActionSets:Actions_BarEnable_OnClick( bar, enable )
	-- Enable or disable a bar
	if ( self:GetTemp(bar) ) then
		if ( enable ) then
			self:SetTemp(bar , 0, nil);
		else
			self:SetTemp(bar , 0, 1);
		end
	end
	self:Actions_UpdateBar( bar )
	SASActionsCheckAll:SetChecked(1);

	self:Actions_SaveEnable();
end

function SimpleActionSets:Actions_DropDown_OnLoad( self )
	UIDropDownMenu_Initialize(self, SimpleActionSets.Actions_DropDown_Initialize);
	UIDropDownMenu_SetWidth(self,135);
	UIDropDownMenu_SetButtonWidth(self,24);
	UIDropDownMenu_JustifyText(self,"LEFT", SASActionSetsDropDown);
	if ( SimpleActionSets:GetCurrentSet() ) then
		UIDropDownMenu_SetSelectedName( self, SASActionSetsDropDown, SimpleActionSets:GetCurrentSet());
	else
		UIDropDownMenu_SetText( self, "|c00999999"..L["SAS_TEXT_CURRENT"], SASActionSetsDropDown );
	end
end

function SimpleActionSets:Actions_DropDown_Initialize( self )
	if ( not SimpleActionSets.db.profile.sets ) then return; end
	local list = {};
	for k, v in pairs(SimpleActionSets.db.profile.sets) do
		tinsert(list, k);
	end
	sort(list);
	local info;
	for k, v in ipairs(list) do
		info = {};
		info.text = v;
		info.value = v;
		info.justifyH = "LEFT";
		info.func = SimpleActionSets.Actions_DropDown_OnClick;
		UIDropDownMenu_AddButton(info);
	end
end

function SimpleActionSets:Actions_DropDown_OnClick()
	UIDropDownMenu_SetSelectedID( SASActionSetsDropDown, self:GetID() );
	SimpleActionSets:Actions_Load(self:GetText());
end

function SimpleActionSets:Actions_Display()
	-- Update the display of all bars.
	if ( self:GetTemp() ) then
		for i=0, 9 do
			self:Actions_UpdateBar( i );
		end
	end
	self:Actions_SaveEnable();
end

function SimpleActionSets:Actions_UpdateBar( bar )
	local SAS_TempBar = self:GetTemp(bar);

	-- Update the display of a bar
	local BarFrame = _G["SASActionBar"..bar];
	if ( SAS_TempBar ) then
		_G[BarFrame:GetName().."Enable"]:SetChecked(not SAS_TempBar[0]);
	end
	if ( bar ) then
		for i=1, 12 do
			self:Actions_UpdateAction( bar, i );
		end
	end

	if ( self:GetDraggingBar() == bar and SASFakeDragFrame:IsVisible() ) then
		BarFrame:SetAlpha(0.5);
	else
		BarFrame:SetAlpha(1.0);
	end
end

function SimpleActionSets:Actions_UpdateAction( bar, id )
	-- Update the display of an action.
	local button = _G["SASActionBar"..bar.."Action"..id]:GetName();
	local icon = _G[button.."Icon"];
	local buttonName = _G[button.."Name"];
	local normalTexture = _G[button.."NormalTexture"];
	local border = _G[button.."Border"];
	local hotkey = _G[button.."HotKey"];
	local enable = self:BarEnabled(bar);
	local SAS_Temp = self:GetTemp();

	if ( self.db.profile.noEmptyButtons and (not SAS_Temp[bar] or not SAS_Temp[bar][id]) ) then
		enable = nil;
	elseif ( not self.db.profile.emptyBars and not self:BarHasActions(bar) ) then
		enable = nil;
	end
	SetDesaturation( _G["SASActionBar"..bar.."Action"..id.."Icon"], not enable );
	border:Hide();
	normalTexture:SetVertexColor(1.0, 1.0, 1.0, 1.0);

	if ( enable ) then
		_G[button]:SetAlpha(1.0);
		border:SetVertexColor(1.0, 0.5, 0.0, 0.2);
		border:Show();

	else
		_G[button]:SetAlpha(0.5);
		border:SetVertexColor(0.5, 1.0, 1.0, 0.2);
		border:Show();
	end

	hotkey:SetTextColor(0.25,0.25,0.25);

	if ( not SAS_Temp ) then
		_G[button]:Disable();
	else
		_G[button]:Enable();

		icon:Hide()
		buttonName:Hide();
		icon:SetVertexColor(1.0, 1.0, 1.0);

		if ( SAS_Temp[bar] and SAS_Temp[bar][id] ) then
			if ( SAS_Temp[bar][id] == 0 ) then
				normalTexture:SetTexture("Interface\\Buttons\\UI-Quickslot");
				border:SetVertexColor(1.0, 0.5, 0.0, 1);
				border:Show();
				icon:SetTexture();
				icon:Show();
			elseif ( SAS_Temp[bar][id] == 1 ) then
				_G[button]:SetAlpha(0.5);
				normalTexture:SetTexture("Interface\\Buttons\\UI-Quickslot2");
				border:SetVertexColor(0.5, 1.0, 1.0, 1);
				border:Show();
				icon:SetTexture();
				icon:Show();
			else
				local name, texture, rank, link, macro = self:ParseActionInfo( SAS_Temp[bar][id] ); --SAS_Temp[bar][id][1], SAS_Temp[bar][id][2], SAS_Temp[bar][id][3], SAS_Temp[bar][id][4], SAS_Temp[bar][id][5];
				icon:SetTexture( texture );
				icon:Show();
				normalTexture:SetTexture("Interface\\Buttons\\UI-Quickslot2");
				hotkey:SetTextColor(0.6,0.6,0.6);

				if ( macro ) then
					buttonName:SetText( name );
					buttonName:Show();
					buttonName:SetTextColor(1.0, 1.0, 1.0);
					if ( not self:FindMacro( name, texture, macro ) ) then
						border:SetVertexColor(1.0, 0.0, 0.0, 0.35);
						buttonName:SetTextColor(0.4, 0.4, 0.4);
						border:Show();
						normalTexture:SetVertexColor(0.9, 0.0, 0.0, 1.0);
						icon:SetVertexColor(0.4, 0.4, 0.4);
					end
				elseif ( link and rank~="summonpet" and rank~="flyout") then
					border:SetVertexColor(0, 1.0, 0, 0.35);
					border:Show();
					local itemLink;
					if ( link == "?" or link == "1" ) then
						itemLink = self:CheckItem( name, bar*12+id, self:Actions_GetLoaded() );
					else
						itemLink = self:FindItem( link );
					end
					if ( not itemLink or itemLink == "?" ) then
						border:SetVertexColor(1.0, 0.0, 0.0, 0.35);
						normalTexture:SetVertexColor(0.9, 0.0, 0.0, 1.0);
						icon:SetVertexColor(0.4, 0.4, 0.4);
					elseif ( link == "?" ) then
						self:SetTemp(bar, id, self:IncActionInfo( SAS_Temp[bar][id], itemLink, 4 ));
					end
				elseif ( rank == "MOUNT" ) then
					if ( not self:FindCompanion( name, rank ) ) then
						icon:SetVertexColor(0.4, 0.4, 0.4);
						border:SetVertexColor(1.0, 0.0, 0.0, 0.35);
					end
				elseif ( rank == "summonpet" ) then
					local _, _, _, _, _, _, battlePetName = C_PetJournal.GetPetInfoByPetID(link);
					if ( not battlePetName ) then
						icon:SetVertexColor(0.4, 0.4, 0.4);
						border:SetVertexColor(1.0, 0.0, 0.0, 0.35);
					end
				elseif ( rank == "equipmentset" ) then
					local equipSetIcon = GetEquipmentSetInfoByName(name);
					if ( not equipSetIcon ) then
						icon:SetVertexColor(0.4, 0.4, 0.4);
						border:SetVertexColor(1.0, 0.0, 0.0, 0.35);
					end
				elseif ( rank == "flyout" ) then
					local _, _, _, isKnown = GetFlyoutInfo(link)
					if ( not isKnown ) then
						icon:SetVertexColor(0.4, 0.4, 0.4);
						border:SetVertexColor(1.0, 0.0, 0.0, 0.35);
					end
				else
					local spellNum = self:FindSpell( name, rank );

					if ( not spellNum ) then
						icon:SetVertexColor(0.4, 0.4, 0.4);
						border:SetVertexColor(1.0, 0.0, 0.0, 0.35);
						border:Show();
					else
						local checktexture = GetSpellTexture( spellNum, BOOKTYPE_SPELL );
						if ( texture ~= checktexture ) then
							icon:SetTexture( checktexture );
							local set = self:Actions_GetLoaded();
							if ( set ) then
								self:SetTemp(bar, id, self:IncActionInfo( SAS_Temp[bar][id], checktexture, 2 ));
								self.db.profile.sets[set][bar][id] = self:IncActionInfo( self.db.profile.sets[set][bar][id], checktexture, 2 );
							end
						end
					end
				end
			end
		elseif ( SAS_Temp[bar] ) then
			if ( enable ) then
				normalTexture:SetTexture("Interface\\Buttons\\UI-Quickslot");
			else
				normalTexture:SetTexture("Interface\\Buttons\\UI-Quickslot2");
			end
		end
	end
end

function SimpleActionSets:Actions_SaveNew( set )
	SASSaveMenuNameEB:SetText("");
	ShowUIPanel(SASSaveMenu);
	if ( set ) then
		SASSaveMenuNameEB:SetText( set );
		self:SaveMenu_Save( set );
	end
end

function SimpleActionSets:Actions_ClearTemp()
	local SAS_Temp = self:GetTemp();
	-- Clear the temp set
	for i=0, 9 do
		SAS_Temp[i] = {};
	end
	self:SetTemp(nil, nil, SAS_Temp);
	self:Actions_Display();
end

function SimpleActionSets:Actions_LoadCurrent()
	if ( SASActionsSave:GetButtonState() == "DISABLED" ) then
		self:Actions_Load();
	else
		self:Warning("UNSAVED_LOAD", function() self:Actions_Load() end);
	end
end

function SimpleActionSets:Actions_SwapSet()
	if ( SASActionsSave:GetButtonState() == "NORMAL" ) then
		self:Warning("SWAPPINGSAVE", function(set) self:Actions_SwapSave(set) end, self:Actions_GetLoaded());
	else
		self:Warning("SWAPPING", function(set) self:SwapSet(set) end, self:Actions_GetLoaded());
	end
end

function SimpleActionSets:Actions_SwapSave( set )
	self:SaveSet( set );
	self:SwapSet( set );
end

function SimpleActionSets:Actions_Delete(set)
	self:Warning("DELETE", function(set) self:Delete(set) end, self:Actions_GetLoaded());
end

function SimpleActionSets:Actions_Save()
	self:Warning("SAVE", function(set) self:SaveSet(set) end, self:Actions_GetLoaded());
end

function SimpleActionSets:Actions_SaveEnable()
	local set = self:Actions_GetLoaded();
	if ( set and self:CompareSet(self:GetTemp(), self.db.profile.sets[set], 2) ) then
		SASActionsSave:Enable();
	else
		SASActionsSave:Disable();
	end
end

function SimpleActionSets:Actions_Cancel( self )
	if ( SASActionsSave:GetButtonState() == "DISABLED" ) then
		HideUIPanel(self:GetParent());
	else
		SimpleActionSets:Warning("UNSAVED_CANCEL", self.Main_Toggle);
	end
end

function SimpleActionSets:Actions_GetLoaded()
	local set = UIDropDownMenu_GetSelectedName( SASActionSetsDropDown );
	if ( set ~= L["SAS_TEXT_CURRENT"] ) then
		return set;
	end
end

function SimpleActionSets:ActionsSave_Enable()
	SASActionsSave.Left:SetTexture("Interface\\Buttons\\UI-Panel-Button-Up");
	SASActionsSave.Middle:SetTexture("Interface\\Buttons\\UI-Panel-Button-Up");
	SASActionsSave.Right:SetTexture("Interface\\Buttons\\UI-Panel-Button-Up");
	SASActionsSave:oldEnable();
	SASActionsSave:EnableMouse(1);
end

function SimpleActionSets:ActionsSave_Disable()
	SASActionsSave.Left:SetTexture("Interface\\Buttons\\UI-Panel-Button-Disabled");
	SASActionsSave.Middle:SetTexture("Interface\\Buttons\\UI-Panel-Button-Disabled");
	SASActionsSave.Right:SetTexture("Interface\\Buttons\\UI-Panel-Button-Disabled");
	SASActionsSave:oldDisable();
	SASActionsSave:EnableMouse();
end

function SimpleActionSets:Sets_Character_DropDown_OnLoad( self )
	UIDropDownMenu_Initialize(self, SimpleActionSets.Sets_Character_DropDown_Initialize);
	UIDropDownMenu_SetWidth(self,160);
	UIDropDownMenu_SetButtonWidth(self,24);
	UIDropDownMenu_JustifyText(self,"LEFT");
	UIDropDownMenu_SetSelectedID(self, 1 );
end

function SimpleActionSets:Sets_Character_DropDown_Initialize()
	local info;
	local list = {};
	for k, v in pairs(SimpleActionSets.db.profiles) do
		if ( k ~= "debug" and k ~= "BackUp" and k ~= SimpleActionSets:GetPlrName() and v.sets ) then
			for j, z in pairs(v.sets) do
				tinsert(list,k);
				break;
			end
		end
	end
	sort(list);
	if ( SimpleActionSets.db.profiles["BackUp"] ) then
		tinsert(list,"BackUp");
	end
	for i=1, #list do
		info = {};
		info.text = list[i];
		info.value = list[i];
		info.justifyH = "LEFT";
		info.func = function(self) SimpleActionSets:Sets_Character_DropDown_OnClick(self) end;
		UIDropDownMenu_AddButton(info);
	end
end

function SimpleActionSets:Sets_Character_DropDown_OnClick( self )
	UIDropDownMenu_SetSelectedName( SASSets_Character_DropDown, self:GetText() );
	UIDropDownMenu_Initialize( SASSets_CharacterSets_DropDown, SimpleActionSets.Sets_CharacterSets_DropDown_Initialize);
	UIDropDownMenu_SetSelectedID( SASSets_CharacterSets_DropDown, 1 );
end

function SimpleActionSets:Sets_CharacterSets_DropDown_OnLoad( self )
	UIDropDownMenu_Initialize(self, SimpleActionSets.Sets_CharacterSets_DropDown_Initialize);
	UIDropDownMenu_SetWidth(self,160);
	UIDropDownMenu_SetButtonWidth(self,24);
	UIDropDownMenu_JustifyText( self,"LEFT" );
	UIDropDownMenu_SetSelectedID( self, 1 );
end

function SimpleActionSets:Sets_CharacterSets_DropDown_Initialize()
	local info;
	local list = {};
	local char = SASSets_Character_DropDownText:GetText();
	if ( SimpleActionSets.db.profiles[char] and SimpleActionSets.db.profiles[char].sets ) then
		for k, v in pairs(SimpleActionSets.db.profiles[char].sets) do
			tinsert(list,k);
		end
	end
	sort(list);
	for i=1, #list do
		info = {};
		info.text = list[i];
		info.value = list[i];
		info.justifyH = "LEFT";
		info.func = function(self) SimpleActionSets:Sets_CharacterSets_DropDown_OnClick(self) end;
		UIDropDownMenu_AddButton(info);
	end
end

function SimpleActionSets:Sets_CharacterSets_DropDown_OnClick( self )
	UIDropDownMenu_SetSelectedID(SASSets_CharacterSets_DropDown, self:GetID() );
end

function SimpleActionSets:Actions_LoadOtherSet()
	local char = SASSets_Character_DropDownText:GetText();
	local set = SASSets_CharacterSets_DropDownText:GetText();
	self:Actions_Load( set, char );
end